//import Vue from 'vue'
//import VueRouter from 'vue-router'
import FrameworkSub from '../views/base/FrameworkSub'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/base/Login.vue'),
  },
  {
    path: '/page404',
    component: () => import('../views/base/Page404.vue'),
  },
  {
    path: '/pageNoPermission',
    component: () => import('../views/base/PageNoPermission.vue'),
  },
  {
    path: '/loading',
    component: () => import('../views/base/Loading.vue'),
  },
  {
    path: '*',
    redirect: '/page404',
  },
  {
    path: '/',
    component: () => import('../views/base/Framework.vue'),
    redirect: '/loading',
    children: [
      {
        path: '/home',
        name: '首页',
        component: () => import('../views/base/Home.vue'),
        meta: {
          code: 'home',
        },
      },
      {
        path: '/settings',
        name: '系统设置',
        redirect: '/page404',
        component: FrameworkSub,
        children: [
          {
            name: '菜单设置',
            path: 'menu',
            component: () => import('../views/settings/Menu.vue'),
            meta: {
              code: 'settings_menu',
            },
          },
          {
            name: '角色管理',
            path: 'role',
            component: () => import('../views/settings/Role.vue'),
            meta: {
              code: 'settings_role',
            },
          },
          {
            name: '账号管理',
            path: 'account',
            component: () => import('../views/settings/Account.vue'),
            meta: {
              code: 'settings_account',
            },
          },
          {
            name: '字典管理',
            path: 'dic',
            component: () => import('../views/settings/Dic.vue'),
            meta: {
              code: 'settings_dic',
            },
          },
          {
            name: '操作日志',
            path: 'operLog',
            component: () => import('../views/settings/OperLog.vue'),
            meta: {
              code: 'settings_operlog',
            },
          },
        ],
      },
      
      {
        path: '/task',
        name: '定时任务管理',
        redirect: '/page404',
        component: FrameworkSub,
        children: [
          {
            name: '定时任务',
            path: 'taskList',
            component: () => import('../views/task/Task.vue'),
            meta: {
              code: 'task_list',
            },
          },
        ],
      },
      //树形结构
      {
        path: '/test',
        name: '多级菜单测试',
        redirect: '/page404',
        component: FrameworkSub,
        children: [
          {
            name: '二级菜单1',
            path: 'test01',
            redirect: '/page404',
            component: FrameworkSub,
            children: [
              {
                name: '三级菜单1_1',
                path: 'test01_01',
                component: () => import('../views/test/Test1_1.vue'),
                meta: {
                  code: 'no_permission',
                },
              },
              {
                name: '三级菜单1_2',
                path: 'test01_02',
                component: () => import('../views/test/Test1_2.vue'),
                meta: {
                  code: 'no_permission',
                },
              },
            ],
          },
        ],
      },
      //线性结构
      {
        path: '/test/test02',
        name: '多级菜单测试_二级菜单2',
        component: () => import('../views/test/Test2.vue'),
        meta: {
          code: 'no_permission',
        },
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

router.beforeEach((to, from, next) => {
  //登录，404 loading 不做任何拦截
  let permissionCode = to.meta.code
  if (permissionCode == null) {
    next()
    return
  }
  let userInfo = sessionStorage.getItem('userInfo')
  userInfo =
    userInfo !== undefined && userInfo !== 'null' && userInfo != ''
      ? JSON.parse(userInfo)
      : null
  let permissionCodeList = userInfo != null ? userInfo.permissionCodeList : []
  //如果sessionStoreage中没有登录信息，跳转到过渡页
  if (userInfo == null || permissionCodeList === undefined) {
    next({
      path: '/loading',
      query: {
        redirect: encodeURIComponent(to.path),
      },
    })
    return
  }
  let haveCode = permissionCodeList.find((item) => {
    return permissionCode != null && permissionCode === item
  })
  if (haveCode === undefined) {
    next({
      path: '/page404',
    })
    return
  }
  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
export default router
